<template>
  <el-table-column :prop="prop" v-bind="$attrs">
    <template slot-scope="scope">
      <span @click.prevent="toggleHandle(scope.$index, scope.row)" :style="childStyles(scope.row, scope.$index)">
        <i :class="iconClasses(scope.row)" :style="iconStyles(scope.row)"></i>
        {{ scope.row[prop] }}
      </span>
    </template>
  </el-table-column>
</template>

<script>
  export default {
    name: 'table-tree-column',
    props: {
      prop: {
        type: String
      },
      treeKey: {
        type: String,
        default: 'catId'
      },
      parentKey: {
        type: String,
        default: 'parentId'
      },
      levelKey: {
        type: String,
        default: '_level'
      },
      childKey: {
        type: String,
        default: 'list'
      }
    },
    methods: {
      childStyles(row, index) {
        console.log("row", row);
        return {
          'padding-left': (row[this.levelKey] > 1 ? row[this.levelKey] * 14 : 0) + 'px',
          'cursor': 'pointer'
          // 'margin-left': '10px'

        }
      },
      iconClasses(row) {
        return [!row._expanded ? 'el-icon-arrow-right' : 'el-icon-arrow-down']
      },
      iconStyles(row) {
        return {
          'visibility': this.hasChild(row) ? 'visible' : 'hidden'
        }
      },
      hasChild(row) {
        return (Array.isArray(row[this.childKey]) && row[this.childKey].length >= 1) || false
      },
      // 切换处理
      toggleHandle(index, row) {
        if (this.hasChild(row)) {
          console.log(row);
          console.log(index);
          console.log("this.$parent", this.$parent);
          var data = this.$parent.store.states.data.slice(0) //当前行下面的数据
          console.log("data", data);
          data[index]._expanded = !data[index]._expanded
          if (data[index]._expanded) {
            data = data.splice(0, index + 1).concat(row[this.childKey]).concat(data)
            console.log("点击之后的树data", data);
          } else {
            data = this.removeChildNode(data, row[this.treeKey])
          }
          this.$parent.store.commit('setData', data)
          // this.$nextTick(() => {
          //   this.$parent.doLayout()
          // })
        }
      },
      // 移除子节点
      removeChildNode(data, parentId) {
        console.log(data,parentId);
        var parentIds = Array.isArray(parentId) ? parentId : [parentId]
        if (parentId.length <= 0) {
          return data
        }
        var ids = []
        for (var i = 0; i < data.length; i++) {
          if (parentIds.indexOf(data[i][this.parentKey]) !== -1 && parentIds.indexOf(data[i][this.treeKey]) === -1) {
            ids.push(data.splice(i, 1)[0][this.treeKey])
            i--
          }
        }
        console.log("ids", ids);
        return this.removeChildNode(data, ids)
      }
    }
  }
</script>
<style lang="less" scoped>
  /deep/.el-table--border td:first-child .cell,
  /deep/.el-table--border th:first-child .cell {
    padding-left: 10px;
  }
</style>
